package common

import (
	"bufio"
	"log"
	"math/rand"
	"os"
	"time"
)

//读取控制台输入
func Ff_input() {
	running := true
	reader := bufio.NewReader(os.Stdin)
	for running {
		data, _, _ := reader.ReadLine()
		command := string(data)
		if command == "stop" {
			running = false
		}
		log.Println("commad: ", command)
	}
}

//生成随机数
func Ff_genrandstr(gtype int, glen int) string {
	var numstr string = ""
	switch {
	case gtype == 1:
		numstr = "0123456789"
	case gtype == 2:
		numstr = "abcdefghijklmnopqrstuvwxyz"
	case gtype == 3:
		numstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	case gtype == 4:
		numstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	case gtype == 5:
		numstr = "0123456789abcdefghijklmnopqrstuvwxyz"
	case gtype == 6:
		numstr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	case gtype == 7:
		numstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	case gtype == 8:
		numstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_+-#$%^&*()!"
	default:
		numstr = "0123456789"
	}

	tmpstr := ""
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < glen; i++ {
		strlen := len(numstr)
		n := r.Intn(strlen)
		tmpstr = tmpstr + string(numstr[n])
	}
	return tmpstr

}

//求两整数之和
func Add(a int, b int) int {
	return a + b
}

//判断文件是否存在
func IsFileExists(filename string) bool {

	f, err := os.Open(filename)
	defer f.Close()
	if err != nil && os.IsNotExist(err) {
		return false
	}

	return true
}

//从1累加指定的整数
func Sum(num int) int {
	var sum int = 0
	for i := 0; i <= num; i++ {
		sum += i
	}
	return sum
}

//阶乘
func Factorial(num int64) int64 {
	var sum int64 = 1
	if num <= 1 {
		sum = 1
	} else {
		sum = num * Factorial(num-1)
	}
	return sum
}
